package classifier;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import primitive.Article;

public class FileReader {
	private String filename;
	private String current_line;
	private String article_buffer;
	private List<Article> articles_list = new ArrayList<>();
	
	public List<Article> get_articles() {
		return articles_list;
	}
	
	public void run(String filename) {
		// Clean old data
		articles_list.clear();
		
		boolean is_buffer_filling = false;
		// Lets open a file
		try {
			FileInputStream fstream = new FileInputStream(filename);
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			
			while ((current_line = br.readLine()) != null) {
				//
				if (current_line.indexOf("<ARTICLE>") != -1) {
					is_buffer_filling = true;
					article_buffer += current_line;
				}
				
				article_buffer += current_line;
				
				if (current_line.indexOf("</ARTICLE>") != -1) {
					is_buffer_filling = false;
					article_buffer += current_line;
					
					articles_list.add(article_creator(article_buffer));	// append article
					
					article_buffer = "";
				}
			}
		} catch (Exception e) {
			//
		}
	}
	
	public Article article_creator(String article_buffer) {	// TODO: test this code...
		Article article = new Article();
		
		// Get DATE
		if (article_buffer.indexOf("<DATE>") == -1) {
			article.setDate("");
		} else {
			article.setDate(article_buffer.substring(article_buffer.indexOf("<DATE>") + 6, article_buffer.indexOf("</DATE>")));
		}
		
		// Get TOPICS
		if (article_buffer.indexOf("<TOPICS>") == -1) {
			article.setTopics("");
		} else {
			article.setTopics(article_buffer.substring(article_buffer.indexOf("<TOPICS>") + 8, article_buffer.indexOf("</TOPICS>")));
		}
		
		// Get PLACES
		if (article_buffer.indexOf("<PLACES>") == -1) {
			article.setPlaces("");
		} else {
			article.setPlaces(
					article_buffer.substring(
							article_buffer.indexOf("<PLACES>") + 8, 
							article_buffer.indexOf("</PLACES>")).replaceAll("<D>", "").replaceAll("</D>", ""));
		}
		
		// Get PEOPLE
		if (article_buffer.indexOf("<PEOPLE>") == -1) {
			article.setPeople("");
		} else {
			article.setPeople(
					article_buffer.substring(
							article_buffer.indexOf("<PEOPLE>") + 8, 
							article_buffer.indexOf("</PEOPLE>")).replaceAll("<D>", "").replaceAll("</D>", ""));
		}
		
		// Get ORGS
		if (article_buffer.indexOf("<ORGS>") == -1) {
			article.setOrgs("");
		} else {
			article.setOrgs(article_buffer.substring(article_buffer.indexOf("<ORGS>") + 6, article_buffer.indexOf("</ORGS>")));
		}
		
		// Get EXCHANGES
		if (article_buffer.indexOf("<EXCHANGES>") == -1) {
			article.setExchanges("");
		} else {
			article.setExchanges(article_buffer.substring(article_buffer.indexOf("<EXCHANGES>") + 11, article_buffer.indexOf("</EXCHANGES>")));
		}
		
		// Get COMPANIES
		if (article_buffer.indexOf("<COMPANIES>") == -1) {
			article.setCompanies("");
		} else {
			article.setCompanies(article_buffer.substring(article_buffer.indexOf("<COMPANIES>") + 11, article_buffer.indexOf("</COMPANIES>")));
		}
		
		// Get UNKNOWN
		if (article_buffer.indexOf("<UNKNOWN>") == -1) {
			article.setUnknown("");
		} else {
			article.setUnknown(article_buffer.substring(article_buffer.indexOf("<UNKNOWN>") + 9, article_buffer.indexOf("</UNKNOWN>")));
		}
		
		// Get TITLE
		if (article_buffer.indexOf("<TITLE>") == -1) {
			article.setTitle("");
		} else {
			article.setTitle(article_buffer.substring(article_buffer.indexOf("<TITLE>") + 7, article_buffer.indexOf("</TITLE>")));
		}
		
		// Get DATELINE
		if (article_buffer.indexOf("<DATELINE>") == -1) {
			article.setDateline("");
		} else {
			article.setDateline(article_buffer.substring(article_buffer.indexOf("<DATELINE>") + 10, article_buffer.indexOf("</DATELINE>")));
		}
		
		// Get BODY
		if (article_buffer.indexOf("<BODY>") == -1) {
			article.setBody("");
		} else {
			article.setBody(article_buffer.substring(article_buffer.indexOf("<BODY>") + 6, article_buffer.indexOf("</BODY>")));
		}
		
		return article;
	}
}
